#!/usr/bin/env python
# encoding: utf-8
# author: 04

import itertools

from django.db import models


def model_stat_fields_create(category_facet, stats_facet, local_space, fix_field={}):
    """ 生成purify或practice的统计项
    填充至传入的locals环境中
    取category和待统计项的笛卡尔积
    """
    for (name, name_cn), (stats, stats_cn) in itertools.product(category_facet, stats_facet):
        field_name = f'{name}_{stats}'
        field_name = fix_field.get(field_name, field_name)
        verbose_name = f'{name_cn}_{stats_cn}'
        local_space[field_name] = models.IntegerField(
            verbose_name,
            default=0,
            blank=True
        )


def serialize_fields_create(field_names, local_space, field_type,
                            write_only=True, required=False, allow_null=False):
    """ 循环生成 serializers field
    填充至传入的locals环境中
    """
    for field_name in field_names:
        local_space[field_name] = field_type(
            default=None,
            allow_null=allow_null,
            required=required,
            write_only=write_only,
        )
